﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
namespace LicPdaService
{
    public class PickUp
    {
        /// <summary>
        /// 2.0扫描出库单号、库位查询
        /// DataSource:Orders
        /// </summary>
        // public static string GetLocPickUp(string xml)
        // {
        //List<string> sqllists = new List<string>();
        //string strSql = string.Empty;
        //App.Connection();
        //App.DBconSet();

        ////1接收分拣信息XML
        //DataTable ldt = LicPdaService.App.Xml2DataTable(xml);
        //if (ldt.Rows.Count == 0)
        //{
        //    return "";
        //}

        ////2获取分配信息
        //strSql = "select  * from pickdetail  where 1=1  and orderkey='" + ldt.Rows[0]["orderkey"].ToString() + "' and fromloc='" + ldt.Rows[0]["loc"].ToString() + "' and status='01' ";
        //DataTable ldt_RecDetail = App.GetDataTable(App.DBconWMS, strSql, "pickdetail");
        //return RecGetPickUps_Return(ldt_RecDetail);
        // }





        //拣货、扫描组成新需要的表
        public static DataTable GetCreateDataTable()
        {
            DataTable ldt_new = new DataTable();
            ldt_new.Columns.Add("orderkey");
            ldt_new.Columns.Add("userid");
            ldt_new.Columns.Add("whseid");
            ldt_new.Columns.Add("sku");
            ldt_new.Columns.Add("po");
            ldt_new.Columns.Add("lottable01");
            ldt_new.Columns.Add("qty");
            ldt_new.Columns.Add("lottable10");
            return ldt_new;
        }



        public static DataTable GetReturnDataTable()
        {
            DataTable ldt_new = new DataTable();
            //ldt_new.Columns.Add("orderkey");
            //ldt_new.Columns.Add("userid");
            //ldt_new.Columns.Add("whseid");
            ldt_new.Columns.Add("MessageType");
            ldt_new.Columns.Add("master_label");
            ldt_new.Columns.Add("return_status");
            return ldt_new;
        }

        public static DataTable GetCreateDataTableTray()
        {
            DataTable ldt_new = new DataTable();
            ldt_new.Columns.Add("sku");
            ldt_new.Columns.Add("po");
            ldt_new.Columns.Add("lottable01");
            ldt_new.Columns.Add("qty");
            ldt_new.Columns.Add("lottable10");
            return ldt_new;
        }

        /// <summary>
        /// 10.0分拣（扫描拣货信息、检验是否存在系统,返回信息）
        /// DataSource:Orders
        /// </summary>
        public static string OrdGetPickUp(string xml)
        {
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;
            App.Connection();
            App.DBconSet();

            //1接收分拣信息XML
         
          //  DataTable ldt = LicPdaService.App.Xml3DataTable(xml);
            DataTable ldts = LicPdaService.App.Xml2DataTable(xml);//取拣货类似
            if (ldts.Rows.Count == 0)
            {
                return "";
            }

            DataTable ldt = LicPdaService.App.Xml3DataTable(xml);//取拣货类似
            if (ldt.Rows.Count == 0)
            {
                return "";
            }




            // DataTable ldt_PickDetail = new DataTable();

            try
            {

                strSql = "select  * from pickdetail  where 1=1  and whseid='" + ldts.Rows[0]["whseid"].ToString() + "'     and orderkey='" + ldts.Rows[0]["orderkey"].ToString() + "'  ";
                strSql += " and  (status='01' or status='03' ) ";
                //strSql += " and lottable01='" + ldt.Rows[0]["lottable01"].ToString() + "' and lottable02='" + ldt.Rows[0]["lottable02"].ToString() + "'  ";
                DataTable ldt_PickDetail = App.GetDataTable(App.DBconWMS, strSql, "pickdetail");
                if (ldt_PickDetail.Rows.Count == 0)
                {
                   // DataTable dts = GetReturnDataTable();
                    //DataRow ldr = dts.NewRow();
                    //ldr["MessageType"] = "PickUp";//出库扫描
                    //ldr["master_label"] = "";//master_label
                    //ldr["return_status"] = "扫描的出库单号不存在对应的厂别";//厂返回状态
                    //dts.Rows.Add(ldr);
                    DataTable dts = new DataTable();


                    return OrdPick_Return(dts);
                }
                else
                {
                    return OrdPick_Return(ldt);
                }
              
            }
            catch (Exception ex)
            {
                #region
                App.Log("OrdGetPickUp", ex.Message.ToString());
                throw;
                #endregion
            }
        


        }

        //出库--分拣信息返回值
        private static string OrdPick_Return(DataTable ldt)
        {
            XmlDocument doc = new XmlDocument();//返回报文
            try
            {
                string return_status = "";
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                doc.AppendChild(dec);
                XmlElement rootRet = doc.CreateElement("NewDataSet");
                if (ldt.Rows.Count != 0)
                {
                    for (int i = 0; i < ldt.Rows.Count; i++)
                    {
                        XmlNode putawayNodes = doc.CreateElement("order_return");//返回用户节点
                        XmlElement lxMessageType = doc.CreateElement("MessageType");
                        XmlElement lxeMasrerlabel = doc.CreateElement("master_label");
                        //XmlElement lxeuserid = doc.CreateElement("userId");
                        //XmlElement lxewhseid = doc.CreateElement("whseid");
                        //XmlElement lxeorderkey = doc.CreateElement("orderkey");
                        //XmlElement lxepo = doc.CreateElement("po");
                        //XmlElement lxesku = doc.CreateElement("sku");
                        //XmlElement lxeqty = doc.CreateElement("qty");
                        //XmlElement lxelottable01 = doc.CreateElement("lottable01");
                        //XmlElement lxelottable10 = doc.CreateElement("lottable10");
                        XmlElement lxereturn_status = doc.CreateElement("return_status");
                        lxMessageType.InnerText = "PickUp";
                        //lxeuserid.InnerText = ldt.Rows[i]["userId"].ToString();
                        //lxewhseid.InnerText = ldt.Rows[i]["whseid"].ToString();
                        //lxeorderkey.InnerText = ldt.Rows[i]["orderkey"].ToString();
                        //lxepo.InnerText = ldt.Rows[i]["po"].ToString();
                        //lxesku.InnerText = ldt.Rows[i]["sku"].ToString();
                        //lxeqty.InnerText = ldt.Rows[i]["qty"].ToString();
                        //lxelottable01.InnerText = ldt.Rows[i]["lottable01"].ToString();
                        //lxelottable10.InnerText = ldt.Rows[i]["lottable10"].ToString();
                        lxeMasrerlabel.InnerText = ldt.Rows[i]["master_label"].ToString();
                        lxereturn_status.InnerText = "OK";
                        putawayNodes.AppendChild(lxMessageType);
                        putawayNodes.AppendChild(lxeMasrerlabel);
                        //putawayNodes.AppendChild(lxeuserid);
                        //putawayNodes.AppendChild(lxewhseid);
                        //putawayNodes.AppendChild(lxeorderkey);
                        //putawayNodes.AppendChild(lxesku);
                        //putawayNodes.AppendChild(lxepo);
                        //putawayNodes.AppendChild(lxelottable01);
                        //putawayNodes.AppendChild(lxeqty);
                        //putawayNodes.AppendChild(lxelottable10);
                        putawayNodes.AppendChild(lxereturn_status);
                        rootRet.AppendChild(putawayNodes);
                    }
                }
                else
                {
                    XmlNode putawayNodes = doc.CreateElement("order_return");//返回用户节点
                    XmlElement lxMessageType = doc.CreateElement("MessageType");
                    XmlElement lxeMasrerlabel = doc.CreateElement("master_label");
                    //XmlElement lxeuserid = doc.CreateElement("userId");
                    //XmlElement lxewhseid = doc.CreateElement("whseid");
                    //XmlElement lxeorderkey = doc.CreateElement("orderkey");
                    //XmlElement lxepo = doc.CreateElement("po");
                    //XmlElement lxesku = doc.CreateElement("sku");
                    //XmlElement lxeqty = doc.CreateElement("qty");
                    //XmlElement lxelottable01 = doc.CreateElement("lottable01");
                    //XmlElement lxelottable10 = doc.CreateElement("lottable10");
                    XmlElement lxereturn_status = doc.CreateElement("return_status");
                    lxMessageType.InnerText = "PickUp";
                    lxeMasrerlabel.InnerText = "";
                    //lxeuserid.InnerText = "";
                    //lxewhseid.InnerText = "";
                    //lxeorderkey.InnerText = "";
                    //lxepo.InnerText = "";
                    //lxesku.InnerText = "";
                    //lxeqty.InnerText = "";
                    //lxelottable01.InnerText = "";
                    //lxelottable10.InnerText = "";
                    lxereturn_status.InnerText = "获取扫描拣货信息失败!可能原因：  1、扫描的料号不存在该出库单中  2、扫描的出库单已经拣货完成 3、扫描的出库单号不存在";
                    putawayNodes.AppendChild(lxMessageType);
                    putawayNodes.AppendChild(lxeMasrerlabel);
                    //putawayNodes.AppendChild(lxeuserid);
                    //putawayNodes.AppendChild(lxewhseid);
                    //putawayNodes.AppendChild(lxeorderkey);
                    //putawayNodes.AppendChild(lxesku);
                    //putawayNodes.AppendChild(lxepo);
                    //putawayNodes.AppendChild(lxelottable01);
                    //putawayNodes.AppendChild(lxeqty);
                    //putawayNodes.AppendChild(lxelottable10);
                    putawayNodes.AppendChild(lxereturn_status);
                    rootRet.AppendChild(putawayNodes);

                }





                doc.AppendChild(rootRet);
                return doc.InnerXml;
            }
            catch (Exception ex)
            {
                App.Log("PDAPickUp.cs-2.0", ex.Message.ToString());
                return doc.InnerXml;
            }

        }

        /// <summary>
        /// 10.1分拣（通过拣货信息-根据拣货扫描信息--生成托盘号）
        /// DataSource:Orders
        /// </summary>
        public static string OrdTray(string xml)
        {
            string whseid = "";
            string userid = "";
            string orderkey = "";
            string pickupkey = "";
            string picktray = "";
            try
            {

         
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;
            App.Connection();
            App.DBconSet();

            //1接收分拣信息XML
           
                DataTable ldts = LicPdaService.App.Xml2DataTable(xml);//扫描出库单号、用户名、厂别
                if (ldts.Rows.Count == 0)
                {
                    //return "";
                    return OrdPickTray_Return(ldts.Rows[0]["whseid"].ToString(), ldts.Rows[0]["userid"].ToString(), "", ldts.Rows[0]["orderkey"].ToString(), "");
                }

                DataTable ldt = LicPdaService.App.Xml3DataTable(xml);//取扫描Masterlabel 信息
                if (ldt.Rows.Count == 0)
                {
                    // return "";
                    return OrdPickTray_Return(ldts.Rows[0]["whseid"].ToString(), ldts.Rows[0]["userid"].ToString(), "", ldts.Rows[0]["orderkey"].ToString(), "");
                }

                DataTable ldt_ys = GetCreateDataTableTray();//声明dt(用于构建)

                strSql = "select  * from pickdetail  where 1=1  and whseid='" + ldts.Rows[0]["whseid"].ToString() + "'    and orderkey='" + ldts.Rows[0]["orderkey"].ToString() + "'  ";
                strSql += " and  (status='01' or status='03' or status='12' ) ";
                DataTable ldt_PickDetail = App.GetDataTable(App.DBconWMS, strSql, "pickdetail");
                if(ldt_PickDetail.Rows.Count==0) //查询是否存在拣货信息
                {
                    return OrdPickTray_Return(ldts.Rows[0]["whseid"].ToString(), ldts.Rows[0]["userid"].ToString(), "", ldts.Rows[0]["orderkey"].ToString(),"");
                }
                if (ldt.Rows.Count != 0)
                {
                    for (int j = 0; j < ldt.Rows.Count; j++)
                    {
                        string[] ls;
                        ls = ldt.Rows[j]["master_label"].ToString().Split('|');
                        DataRow ldr = ldt_ys.NewRow();
                        if (ls.Length == 5)
                        {
                            ldr["sku"] = ls[0].ToString();//料号
                            ldr["po"] = ls[1].ToString(); //订单号
                            ldr["lottable01"] = ls[2].ToString(); //批次
                            ldr["qty"] = ls[3].ToString(); //数量
                            ldr["lottable10"] = ls[4].ToString(); //供应商
                        }
                        if (ls.Length == 4)
                        {
                            ldr["sku"] = ls[0].ToString();//料号
                            ldr["po"] = ls[1].ToString(); //订单号
                            ldr["lottable01"] = ls[2].ToString(); //批次
                            ldr["qty"] = ls[3].ToString(); //数量
                            ldr["lottable10"] = "";
                        }
                        ldt_ys.Rows.Add(ldr);

                    }
                }

                for (int m = 0; m < ldt_ys.Rows.Count; m++)
                {
                    string sku = ldt_ys.Rows[m]["sku"].ToString();
                    string strSql_pk = " select * from pickdetail where 1=1  and whseid='" + ldts.Rows[0]["whseid"].ToString() + "'    and orderkey='" + ldts.Rows[0]["orderkey"].ToString() + "'   ";
                    strSql_pk += "  and  (status='01' or status='03'or status='12' )   and  sku='" + sku+"' ";
                    DataTable ldt_sku = App.GetDataTable(App.DBconWMS, strSql_pk, "sku");
                    if (ldt_sku.Rows.Count <= 0)
                    {
                        return OrdPickTray_Return(ldts.Rows[0]["whseid"].ToString(), ldts.Rows[0]["userid"].ToString(), "", ldts.Rows[0]["orderkey"].ToString(),sku);
                    }
                }
               
                    bool flag = false;
                strSql = "DECLARE @key varchar(50) exec pro_GetOrderNo '" + ldts.Rows[0]["whseid"].ToString() + "','B','PICKTRAY','扫描打托编号',@key OUTPUT select @key as PICKTRAY"; //调用存储过程pro_GetLot生成托盘号;
                DataTable dtpicktray = App.GetDataTable(App.DBconWMS, strSql, "lotTray");
                if (dtpicktray.Rows.Count > 0)
                {
                    picktray = dtpicktray.Rows[0]["PICKTRAY"].ToString();
                }
     
            for (int i = 0; i < ldt.Rows.Count; i++)
            {

                    strSql = "DECLARE @key varchar(50) exec pro_GetLot '" + ldts.Rows[0]["whseid"].ToString() + "','pickupkey','扫描拣货编号',@key OUTPUT select @key as pickupkey";//调用存储过程pro_GetLot拣货编号;
                    DataTable dtpickupke = App.GetDataTable(App.DBconWMS, strSql, "lotPick");
                    if (dtpickupke.Rows.Count > 0)
                    {
                        pickupkey = dtpickupke.Rows[0]["pickupkey"].ToString();
                    }


                 whseid = ldts.Rows[0]["whseid"].ToString();//厂别
                 userid = ldts.Rows[0]["userid"].ToString();//用户名
                 orderkey = ldts.Rows[0]["orderkey"].ToString();//出库单号
                                                                //string customer_code = ldt.Rows[i]["customer_code"].ToString();//厂别(客户)
                    string customer_code = "";
                    if (whseid == "wh1")
                    {
                        customer_code = "MGNYCL";//厂别(客户)
                    }
                    if (whseid == "wh2")
                    {
                        customer_code = "MGNYCL";//厂别(客户)
                    }
                    if (whseid == "wh3")
                    {
                        customer_code = "MGNYCL";//厂别(客户)
                    }


                    // 构建验收DataTable
                    //for (int j = 0; j < ldt.Rows.Count; j++)
                    //{
                    //    string[] ls;
                    //    ls = ldt.Rows[j]["master_label"].ToString().Split('|');
                    //    // DataRow[] ldr = null;
                    //    DataRow ldr = ldt_ys.NewRow();
                    //    ldr["sku"] = ls[0].ToString();//料号
                    //    ldr["po"] = ls[1].ToString(); //订单号
                    //    ldr["lottable01"] = ls[2].ToString(); //批次
                    //    ldr["qty"] = ls[3].ToString(); //数量
                    //    ldr["lottable10"] = ls[4].ToString(); //供应商
                    //    ldt_ys.Rows.Add(ldr);
                    //}


                    string sku = ldt_ys.Rows[i]["sku"].ToString();//料号
                    string strStr = "select description,unit,packey from sku  where whseid='" + whseid+"' and customer_code='"+customer_code+"' and    sku='" + sku + "'";
                    DataTable dts = App.GetDataTable(App.DBconWMS, strStr, "lotTray");
                    string skudescr = dts.Rows[0]["description"].ToString().Trim();//料号描述
                    string uom = dts.Rows[0]["unit"].ToString().Trim();
                   // string skudescr = ldt.Rows[i]["skudescr"].ToString();//料号
               // string uom = ldt.Rows[i]["uom"].ToString();//单位
                double qty = double.Parse(ldt_ys.Rows[i]["qty"].ToString());//数量
                string lottable01 = ldt_ys.Rows[i]["lottable01"].ToString();//批次
                string lottable02 = ldt_ys.Rows[i]["po"].ToString();//订单号
                string lottable10 = ldt_ys.Rows[i]["lottable10"].ToString();//供应商代码
                strSql = "insert into picktray(whseid,pickupkey,traykey,orderkey,customer_code,sku,skudescr, ";
                strSql += "status,uom,qty, lottable01,lottable02,lottable10,addwho,adddate,flag) values";
                strSql += "('" + whseid + "','" + pickupkey + "','" + picktray + "','" + orderkey + "','" + customer_code + "','" + sku + "','" + skudescr + "'";
                strSql += ",'0','" + uom + "','" + qty + "','" + lottable01 + "','" + lottable02 + "','" + lottable10 + "','" + userid + "','" + DateTime.Now + "','0')";
                sqllists.Add(strSql);//记录PDA拣货

            }
            if (App.ExecuteSQLScriptLists(sqllists, App.DBconWMS))
            {
                 flag = true;
            }
                return OrdPickTray_Return(whseid, userid, picktray, orderkey,"");

            }
            catch (Exception ex)
            {
                #region
                App.Log(" PDAXmlPick,10.3,OrdTray", ex.Message.ToString());
                throw;
                #endregion
            }
           

        }

        //出库--生成托标签返回
        private static string OrdPickTray_Return(string whseid, string userid, string picktray,string orderkey,string sku)
        {
            XmlDocument doc = new XmlDocument();//返回报文
            try
            {
                string return_status = "";
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                doc.AppendChild(dec);
                XmlElement rootRet = doc.CreateElement("NewDataSet");
                if (picktray != "")
                {
                    XmlNode putawayNodes = doc.CreateElement("order_return");//返回用户节点
                    XmlElement lxMessageType = doc.CreateElement("MessageType");
                    XmlElement lxlpt = doc.CreateElement("picktray");
                    XmlElement lxemsg = doc.CreateElement("return_status");
                    lxMessageType.InnerText = "PickUpTray";
                    lxlpt.InnerText = picktray;
                    lxemsg.InnerText = "OK";
                    putawayNodes.AppendChild(lxMessageType);
                    putawayNodes.AppendChild(lxlpt);
                    putawayNodes.AppendChild(lxemsg);
                    rootRet.AppendChild(putawayNodes);

                }
                else
                {
                    XmlNode putawayNodes = doc.CreateElement("order_return");//返回用户节点
                    XmlElement lxMessageType = doc.CreateElement("MessageType");
                    XmlElement lxlpt = doc.CreateElement("picktray");
                    XmlElement lxemsg = doc.CreateElement("return_status");
                    lxMessageType.InnerText = "PickUpTray";
                    lxlpt.InnerText = picktray;
                    lxemsg.InnerText = "1、扫描的出库单号在系统中不存在"+orderkey+"，2、扫描的出库单号已经发货"+orderkey+",3扫描的料号不在出库单中"+sku+"";
                    putawayNodes.AppendChild(lxMessageType);
                    putawayNodes.AppendChild(lxlpt);
                    putawayNodes.AppendChild(lxemsg);
                    rootRet.AppendChild(putawayNodes);
                }

                doc.AppendChild(rootRet);
                return doc.InnerXml;


            }

            catch (Exception ex)
            {
                App.Log("PDAPickUp.cs-2.0", ex.Message.ToString());
                return doc.InnerXml;
            }
          
        }

        /// <summary>
        /// 10.2分拣（打托托盘标签）
        /// DataSource:Orders
        /// </summary>
        public static string OrderPickTrayPrint(string xml)
        {

            try
            {

          
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;
            App.Connection();
            App.DBconSet();

            //1接收分拣信息XML
            DataTable ldt = LicPdaService.App.Xml2DataTable(xml);
            if (ldt.Rows.Count == 0)
            {
                return "";
            }

            string     whseid = ldt.Rows[0]["whseid"].ToString();//厂别
            string userid = ldt.Rows[0]["userid"].ToString();//用户名
            string orderkey = ldt.Rows[0]["orderkey"].ToString();//出库单号
            string lpt = ldt.Rows[0]["lpt"].ToString();//托盘号
            return OrderPickTrayPrint_Return(whseid, userid, orderkey, lpt);
            }

            catch (Exception ex)
            {
                #region
                App.Log("OrdTray", ex.Message.ToString());
                throw;
                #endregion
            }


        }

        //出库--打印标签
        private static string OrderPickTrayPrint_Return(string whseid, string userid, string orderkey, string lpt)
        {
            XmlDocument doc = new XmlDocument();//返回报文
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            XmlElement rootRet = doc.CreateElement("NewDataSet");
            XmlNode putawayNodes = doc.CreateElement("order_return");//返回用户节点
            XmlElement lxMessageType = doc.CreateElement("MessageType");
            //XmlElement lxuserid = doc.CreateElement("userid");
            //XmlElement lxwhseid = doc.CreateElement("whseid");
            //XmlElement lxorderkey = doc.CreateElement("orderkey");
            XmlElement lxlpt = doc.CreateElement("lpt");
            XmlElement lxemsg = doc.CreateElement("return_status");
            //lxuserid.InnerText = whseid;
            //lxwhseid.InnerText = userid;
            //lxorderkey.InnerText = orderkey;
            lxMessageType.InnerText = "PickUpPrint";
            lxlpt.InnerText = lpt;
            lxemsg.InnerText = "OK";
            //putawayNodes.AppendChild(lxuserid);
            //putawayNodes.AppendChild(lxwhseid);
            //putawayNodes.AppendChild(lxorderkey);
            putawayNodes.AppendChild(lxMessageType);
            putawayNodes.AppendChild(lxlpt);
            putawayNodes.AppendChild(lxemsg);
            rootRet.AppendChild(putawayNodes);
            doc.AppendChild(rootRet);
            return doc.InnerXml;
        }


        //出库--分拣信息返回值
        private static string RecGetPickUp_Return(DataTable ldt)
        {
            XmlDocument doc = new XmlDocument();//返回报文
            try
            {
                string return_status = "";
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                doc.AppendChild(dec);
                XmlElement rootRet = doc.CreateElement("NewDataSet");
                if (ldt.Rows.Count != 0)
                {
                    for (int i = 0; i < ldt.Rows.Count; i++)
                    {
                        XmlNode putawayNodes = doc.CreateElement("Pickup_return");//返回用户节点
                        XmlElement lxeorderkey = doc.CreateElement("orderkey");
                        XmlElement lxeloc = doc.CreateElement("loc");
                        XmlElement lxesku = doc.CreateElement("sku");
                        XmlElement lxeqty = doc.CreateElement("qty");
                        XmlElement lxereturn_status = doc.CreateElement("return_status");
                        lxeorderkey.InnerText = ldt.Rows[i]["orderkey"].ToString();
                        lxeloc.InnerText= ldt.Rows[i]["fromloc"].ToString();
                        lxesku.InnerText = ldt.Rows[i]["sku"].ToString();
                        lxeqty.InnerText = ldt.Rows[i]["qty"].ToString();
                        lxereturn_status.InnerText = return_status;
                        putawayNodes.AppendChild(lxeorderkey);
                        putawayNodes.AppendChild(lxeloc);
                        putawayNodes.AppendChild(lxesku);
                        putawayNodes.AppendChild(lxeqty);
                        putawayNodes.AppendChild(lxereturn_status);
                        rootRet.AppendChild(putawayNodes);
                    }
                }
                else
                {
                    XmlNode putawayNodes = doc.CreateElement("Pickup_return");//返回用户节点
                    XmlElement lxeorderkey = doc.CreateElement("orderkey");
                    XmlElement lxeloc = doc.CreateElement("loc");
                    XmlElement lxesku = doc.CreateElement("sku");
                    XmlElement lxeqty = doc.CreateElement("qty");
                    XmlElement lxereturn_status = doc.CreateElement("return_status");
                    lxeorderkey.InnerText = "";
                    lxeloc.InnerText = "";
                    lxesku.InnerText = "";
                    lxeqty.InnerText = "";
                    lxereturn_status.InnerText = "获取拣货信息失败!可能原因： 1、扫描的库位与实际分配的不符  2、扫描的出库单已经拣货完成";
                    putawayNodes.AppendChild(lxeorderkey);
                    putawayNodes.AppendChild(lxeloc);
                    putawayNodes.AppendChild(lxesku);
                    putawayNodes.AppendChild(lxeqty);
                    putawayNodes.AppendChild(lxereturn_status);
                    rootRet.AppendChild(putawayNodes);

                }





                doc.AppendChild(rootRet);
                return doc.InnerXml;
            }
            catch (Exception ex)
            {
                App.Log("PDAPickUp.cs-2.0", ex.Message.ToString());
                return doc.InnerXml;
            }

        }
        //出库--分拣信息返回值
        private static string RecGetPickUps_Return(DataTable ldt)
        {
            XmlDocument doc = new XmlDocument();//返回报文
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            XmlElement rootRet = doc.CreateElement("NewDataSet");
            if(ldt.Rows.Count!=0)
            {
                //for (int i = 0; i < ldt.Rows.Count; i++)
                //{
                    XmlNode putawayNodes = doc.CreateElement("Pickup_return");//返回用户节点
                    //XmlElement lxeorderkey = doc.CreateElement("orderkey");
                    //XmlElement lxewhseid = doc.CreateElement("whseid");
                    //XmlElement lxesku = doc.CreateElement("sku");
                    //XmlElement lxeqty = doc.CreateElement("qty");
                    XmlElement lxemsg = doc.CreateElement("msg");

                    //lxeorderkey.InnerText = ldt.Rows[i]["orderkey"].ToString();
                    //lxewhseid.InnerText = ldt.Rows[i]["whseid"].ToString();
                    //lxesku.InnerText = ldt.Rows[i]["sku"].ToString();
                    //lxeqty.InnerText = ldt.Rows[i]["qty"].ToString();
                    lxemsg.InnerText = "OK";
                    //putawayNodes.AppendChild(lxeorderkey);
                    //putawayNodes.AppendChild(lxewhseid);
                    //putawayNodes.AppendChild(lxesku);
                    //putawayNodes.AppendChild(lxeqty);
                    putawayNodes.AppendChild(lxemsg);
                    //putawayNodes.AppendChild(lxereturn_status);
                    rootRet.AppendChild(putawayNodes);
                //}
            }

           


            doc.AppendChild(rootRet);
            return doc.InnerXml;
        }

        //出库--分拣信息返回值
        private static string PickUp_Return(bool  flag)
        {
            XmlDocument doc = new XmlDocument();//返回报文
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            XmlElement rootRet = doc.CreateElement("NewDataSet");
            if (flag==true)
            {
                XmlNode putawayNodes = doc.CreateElement("Pickup_return");//返回用户节点                                                    
                XmlElement lxemsg = doc.CreateElement("msg");
                lxemsg.InnerText = "OK";
                putawayNodes.AppendChild(lxemsg);
                rootRet.AppendChild(putawayNodes);
            }
            if (flag == false)
            {
                XmlNode putawayNodes = doc.CreateElement("Pickup_return");//返回用户节点                                                    
                XmlElement lxemsg = doc.CreateElement("msg");
                lxemsg.InnerText = "失败";
                putawayNodes.AppendChild(lxemsg);
                rootRet.AppendChild(putawayNodes);
            }



            doc.AppendChild(rootRet);
            return doc.InnerXml;
        }


        private static bool UpDatePickdetail(DataTable ldt,DataTable ldts)
        {
            List<string> sqllists = new List<string>();
            bool flag = false;
            string strSql = "";
            App.Connection();
            App.DBconSet();
            
            for (int i = 0; i < ldt.Rows.Count; i++)
            {
                string whseid= ldt.Rows[i]["whseid"].ToString();
                string orderkey = ldt.Rows[i]["orderkey"].ToString();
                string line_no = ldt.Rows[i]["line_no"].ToString();
                string pickdetailkey = ldt.Rows[i]["pickdetailkey"].ToString();
                double qty = double.Parse(ldt.Rows[i]["qty"].ToString());
                string customer_code = ldt.Rows[i]["customer_code"].ToString();
                string status = ldt.Rows[i]["status"].ToString();
                string sku = ldt.Rows[i]["sku"].ToString();
                string lot = ldt.Rows[i]["lot"].ToString();
                string loc = ldt.Rows[i]["fromloc"].ToString();
                string lpn = ldt.Rows[i]["lpn"].ToString();
                string addwho= ldt.Rows[i]["addwho"].ToString();


                strSql = "update pickdetail set status='03',editdate='" + DateTime.Now + "'  where whseid='" + whseid + "' and pickdetailkey='" + pickdetailkey + "'";
                sqllists.Add(strSql); //更新拣货状态
                status = "06";//拣货中状态

                strSql = "update orderdetail set allocated_qty=isnull(allocated_qty,0)-" + qty + ", picked_qty=picked_qty+" + qty + ", status='" + status + "',editdate='" + DateTime.Now + "' ";
                strSql += " where whseid='" + whseid + "' and orderkey='" + orderkey + "' and line_no='" + line_no + "'";
                sqllists.Add(strSql);//更新出库明细状态、拣货数量
             
                strSql = "update inventory set allocated_qty=isnull(allocated_qty,0)-" + qty + ",picked_qty=isnull(picked_qty,0)+" + qty + "  where whseid='" + whseid + "' and customer_code='" + customer_code + "' and sku='" + sku + "' and lot='" + lot + "' and loc='" + loc + "' and lpn='" + lpn + "' ";
                sqllists.Add(strSql);//更新库存拣货数量

                strSql = " update orders  set status = '" + status + "',editdate='" + DateTime.Now + "'  where whseid = '" + whseid + "' and orderkey = '" + orderkey + "'";
                sqllists.Add(strSql);//更新出库表头状态

               

                strSql = "insert into OrdHistoryDtl(whseid,orderkey,line_no,status,operation,addwho,adddate) values ";
                strSql += "('" + whseid + "','" + orderkey + "','" + line_no + "','" + status + "','DAP拣货确认','" + ldts.Rows[0]["username"].ToString() + "','" + DateTime.Now + "')";
                sqllists.Add(strSql);//记录PDA拣货

               

            }
            if (App.ExecuteSQLScriptLists(sqllists, App.DBconWMS))
            {
                flag = true;
            }
         

          
            return flag;
        }

    }
}
